<template>
  <el-card>
    <el-table
      :data="menu.children"
      row-key="id"
      border
      :expand-row-keys="['1']"
    >
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="权限值" prop="code"></el-table-column>
      <el-table-column label="跳转权限值" prop="toCode"></el-table-column>
      <el-table-column label="操作" width="220px" align="center">
        <template #default="{ row }">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-plus"
            title="添加菜单"
            @click="addMenu(row)"
            :disabled="row.level === 4"
          ></el-button>
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            title="修改菜单"
            @click="editMenu(row)"
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            title="删除"
            @click="delMenu(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加菜单" :visible.sync="dialogVisible">
      <el-form
        label-width="120px"
        size="small"
        :model="formData"
        :rules="rules"
        ref="formRef"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="功能权限值" prop="code">
          <el-input v-model="formData.code"></el-input>
        </el-form-item>
        <el-form-item
          label="跳转权限值"
          prop="toCode"
          v-if="formData.level > 2"
        >
          <el-input v-model="formData.toCode"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit('formRef')"> 确 定 </el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  getMenuApi,
  addMenuApi,
  updateMenuApi,
  delMenuApi
} from '@/api/acl/permissionApi'
export default {
  name: 'Menu',
  data() {
    return {
      menu: {},
      dialogVisible: false,
      formData: {
        name: '',
        code: '',
        toCode: '',
        pid: '',
        id: '',
        level: 0
      },
      rowData: {
        name: '',
        toCode: '',
        id: '',
        pid: '',
        level: ''
      },
      rules: {
        name: [{ required: true, message: '请输入名称' }],
        code: [{ required: true, message: '请输入功能权限值' }],
        toCode: [{ required: true, message: '请输入跳转权限值' }]
      }
    }
  },
  mounted() {
    this.getMenu()
  },
  methods: {
    // 提交表单
    submit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.formData.id) {
            await updateMenuApi(this.formData)
            this.$message.success('修改成功')
          } else {
            await addMenuApi({ ...this.formData })
            this.$message.success('添加成功')
          }
          this.dialogVisible = false
          this.getMenu()
        }
      })
    },
    // 删除菜单
    async delMenu(row) {
      await delMenuApi(row.id)
      this.$message.success('删除成功')
      this.getMenu()
    },
    // 编辑菜单
    editMenu(data) {
      this.formData.name = data.name
      this.formData.code = data.code
      this.formData.toCode = data.toCode
      this.formData.level = data.level
      this.dialogVisible = true
      this.formData.id = data.id
      this.formData.pid = data.pid
    },
    // 添加菜单
    addMenu(data) {
      this.formData.pid = data.id
      this.formData.id = ''
      this.formData.name = ''
      this.formData.code = ''
      this.formData.toCode = ''
      this.formData.level = data.level
      this.dialogVisible = true
    },
    // 封装获取菜单的方法
    async getMenu() {
      const { data } = await getMenuApi()
      this.menu = data
    }
  }
}
</script>

<style></style>
